// SPDX-License-Identifier: GPL-3.0-or-later

import QtQuick
import QtQuick.Layouts
import Fk
import Fk.Components.LunarLTK

ColumnLayout {
  id: root
  anchors.fill: parent
  property var extra_data: ({ name: "", owner: Self.id, data: { all: [], pos: 0 } })
  signal finish()

  BigGlowText {
    Layout.fillWidth: true
    Layout.preferredHeight: childrenRect.height + 4

    text: Lua.tr(extra_data.name)
  }

  Flickable {
    id: flp
    flickableDirection: Flickable.HorizontalFlick
    Layout.preferredWidth: Math.min(root.width - root.width % 97)
    anchors.top: title.bottom
    anchors.topMargin: 20
    Layout.fillHeight: true
    Layout.alignment: Qt.AlignHCenter
    contentWidth: rec.width
    contentX: contentWidth / 2
    clip: true

    Item {
      id: rec
      width: {
        const count = cardRepeater.count;
        const index = extra_data.data.pos - 1;
        let ret = delerow.width;
        if (index < 0 || index >= cardRepeater.count)
            ret = ret + 101;
        else if (index === cardRepeater.count - 1) ret = ret + 24;
        return ret > flp.width ? ret : flp.width;
      }
      // height: 512

      RowLayout {
        id: delerow
        spacing: 8
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.horizontalCenterOffset: {
          const count = cardRepeater.count;
          const index = extra_data.data.pos - 1;
          let ret = 0;
          if (index < 0) ret = ret + 101;
          else if (index >= cardRepeater.count) ret = ret - 101;
          else if (index === cardRepeater.count - 1) ret = ret + 24;
          return ret / 2;
        }
        Repeater {
          id: cardRepeater
          model: extra_data.data.all

          delegate: GeneralCardItem {
            Layout.rightMargin: (extra_data.data.pos - 1 === index) ? 24 : 0
            id: cardItem
            autoBack: false
            name: modelData
            onClicked: { // FIXME: rightClicked不能覆写
              roomScene.startCheat("GeneralDetail", { generals: [modelData] });
            }
          }
        }
      }

      GeneralCardItem {
        id: host
        anchors.left: delerow.left
        anchors.leftMargin: {
          const count = cardRepeater.count;
          const index = extra_data.data.pos - 1;
          const wid = 93 + 8;
          if (index < 0) {
            return -wid;
          } else if (index >= count) {
            return count * wid;
          }

          let ret = index * wid;
          return ret + 24;
        }
        y: {
          const count = cardRepeater.count;
          const index = extra_data.data.pos;
          return (index <= 0 || index > count) ? 0 : 24;
        }
        name: roomScene.getPhoto(extra_data.owner).general
        onClicked: { // FIXME: rightClicked不能覆写
          roomScene.startCheat("GeneralDetail", { generals: [name] });
        }
      }
    }
  }
}
